"""
Case Type   : 内核告警
Case Name   : 设置磁盘阈值, 数据库可读模式, 测试告警日志信息
Create At   : 2025-10-10
Owner       : lvlintao
Description :
    1、设置磁盘阈值, 数据库可读模式
    2、测试告警日志信息
    3、清理环境
Expect      :
    1、设置磁盘阈值, 数据库可读模式;
    2、告警日志完整清晰
    3、清理环境成功
History     :
"""

import os
import unittest
import time

from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

logger = Logger()


class AlarmReportTestCase(unittest.TestCase):
    def setUp(self):
        logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.comsh = CommonSH('PrimaryDbUser')
        self.comm = Common('PrimaryDbUser')
        self.node = Node('PrimaryDbUser')
        status = self.comsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)

    def test_alarm_report(self):
        text = '--step1:设置磁盘阈值, 数据库可读模式,;expect:成功--'
        logger.info(text)
        pid = self.comm.get_pid(self.node, f'{macro.DB_INSTANCE_PATH}')
        logger.info(pid)
        dir = f"{macro.DB_INSTANCE_PATH}".split('/')[0]
        cmd = f"df -h /{dir} | grep -v Filesystem | awk '{{{{print $5}}}}'"
        logger.info(cmd)
        res = self.node.sh(cmd).result()
        logger.info(res)
        set_param = int(res.split('%')[0]) - 10
        res = self.comsh.exec_cm_ctl_set('server', f'datastorage_threshold_value_check={set_param}')
        logger.info(res)
        time.sleep(20)
        status = self.comsh.get_db_cluster_status(param='detail')
        logger.info(status)
        self.assertTrue("Primary ReadOnly" in status)

        text = '--step2:查看告警日志;expect:成功--'
        cmd = f"cd {macro.PG_LOG_PATH}/../cm/cm_server;ls -l | grep 'system_alarm' | awk '{{{{print $NF}}}}'"
        logger.info(cmd)
        file = self.node.sh(cmd).result()
        logger.info(file)
        cmd = f"cd {macro.PG_LOG_PATH}/../cm/cm_server;tail -n 20 {file}"
        res = self.node.sh(cmd).result()
        logger.info(res)
        flag = 'TransactionReadOnly'
        self.assertIn(flag, res, '执行失败' + text)

    def tearDown(self):
        text = '--step3:清理环境，恢复默认值;expect:成功--'
        logger.info(text)
        res = self.comsh.exec_cm_ctl_set('server', 'datastorage_threshold_value_check=85')
        logger.info(res)
        time.sleep(10)
        cmd = f"source {macro.DB_ENV_PATH};gs_ctl query |grep local_role"
        res = self.node.sh(cmd).result()
        logger.info(res)
        if 'Primary' not in res:
            cmd = f"source {macro.DB_ENV_PATH};gs_ctl switchover;"
            res = self.node.sh(cmd).result()
            logger.info(res)
            cmd = f"source {macro.DB_ENV_PATH};gs_ctl query |grep local_role"
            res = self.node.sh(cmd).result()
            logger.info(res)
        self.assertIn('Primary', res, '执行失败' + text)
        logger.info(f'-----{os.path.basename(__file__)} end-----')
